package com.Action;

import com.Services.UserService;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by Administrator on 2017/3/15.
 * 该动作用于找回密码
 */
public class FindPasswordAction extends ActionSupport {
    private String userName;
    private String superpassword;
    private String password;
    private String confirmPassword;
    private String yzm;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSuperpassword() {
        return superpassword;
    }

    public void setSuperpassword(String superpassword) {
        this.superpassword = superpassword;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getConfirmPassword() {
        return confirmPassword;
    }

    public void setConfirmPassword(String confirmPassword) {
        this.confirmPassword = confirmPassword;
    }

    public String getYzm() {
        return yzm;
    }

    public void setYzm(String yzm) {
        this.yzm = yzm;
    }

    @Override
    public String execute() throws Exception {
        HttpServletRequest request= ServletActionContext.getRequest();
        request.setCharacterEncoding("utf-8");
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService us=ac.getBean(UserService.class);
        if( yzm.equals(request.getSession().getAttribute("random"))){
            String flag=us.findpassword(userName,superpassword,password,confirmPassword);
            if(flag.equals("success")){
                return SUCCESS;
            }else if(flag.equals("user")){
                request.setAttribute("errorMessage","提示:用户名不存在！");
            }else if(flag.equals("super")){
                request.setAttribute("errorMessage","提示:超级密码错误！");
            }else if(flag.equals("pass")){
                request.setAttribute("errorMessage","提示:两次密码不同！");
            }
        }else{
            request.setAttribute("errorMessage","提示:验证码错误！");
        }
        return ERROR;
    }
}
